package com.itcast.donkey.adapters;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itcast.donkey.R;
import com.itcast.donkey.modles.ScenicAttackModles;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 16/8/30.
 */
public class InAttackItemAdapter extends RecyclerView.Adapter<InAttackItemAdapter.InAttackViewHolder> {

    private Context context;
    private List<ScenicAttackModles.PagesBean.ChildrenBean> childrenBeanList;
    private boolean isShow = true;
    private InAttackShowAdapter adapter;
    private List<Boolean> isShowList = new ArrayList<>();

    public InAttackItemAdapter(Context context, List<ScenicAttackModles.PagesBean.ChildrenBean> childrenBeanList) {
        this.context = context;
        this.childrenBeanList = childrenBeanList;
    }

    @Override
    public InAttackViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_in_attack, null);
        InAttackViewHolder holder = new InAttackViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final InAttackViewHolder holder, final int position) {
        holder.tvTitleInAttack.setText(childrenBeanList.get(position).getTitle());
        final RecyclerView recyclerView = new RecyclerView(context);
        isShowList.add(isShow);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isShowList.get(position)) {
                    holder.linInAttack.removeView(recyclerView);
                    holder.ivPullInAttack1.setBackgroundResource(R.mipmap.icon_destination_brief_title_arrows_up);
                    holder.linInAttack.setVisibility(View.VISIBLE);
                    adapter = new InAttackShowAdapter(context, childrenBeanList.get(position).getSections());
                    recyclerView.setAdapter(adapter);
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
                    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    recyclerView.setLayoutManager(linearLayoutManager);
                    holder.linInAttack.addView(recyclerView);
                    isShowList.set(position, false);
                } else {
                    holder.ivPullInAttack1.setBackgroundResource(R.mipmap.icon_destination_brief_title_arrows_down);
                    holder.linInAttack.setVisibility(View.GONE);
                    holder.linInAttack.removeView(recyclerView);
                    isShowList.set(position, true);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return childrenBeanList.size();
    }

    public class InAttackViewHolder extends RecyclerView.ViewHolder {

        private RelativeLayout relaInAttack1;
        private ImageView ivInAttack1;
        private TextView tvTitleInAttack;
        private ImageView ivPullInAttack1;
        private LinearLayout linInAttack;

        public InAttackViewHolder(View itemView) {
            super(itemView);
            relaInAttack1 = (RelativeLayout) itemView.findViewById(R.id.rela_in_attack1);
            ivInAttack1 = (ImageView) itemView.findViewById(R.id.iv_in_attack1);
            tvTitleInAttack = (TextView) itemView.findViewById(R.id.tv_title_in_attack);
            ivPullInAttack1 = (ImageView) itemView.findViewById(R.id.iv_pull_in_attack1);
            linInAttack = (LinearLayout) itemView.findViewById(R.id.lin_in_attack);
        }
    }
}
